package com.xbongbong.pay.platform.alipay.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import lombok.Getter;
import lombok.Setter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.Serializable;

/**
 * 支付宝设置 元数据
 * @author 魏荣杰
 * @date 2021/1/15 16:34
 * @since
 * @version
 */
@Getter
@Setter
public class AliPayMeta implements Serializable {
    private static final Logger LOG = LoggerFactory.getLogger(AliPayMeta.class);


    private static final long serialVersionUID = 7195306030475118760L;

    /**
     * 商户id
     */
    private String mchId;
    /**
     * 是否证书模式
     */
    private boolean certModel;
    /**
     * 支付宝配置
     */
    private AliPayConfig.Config aliPayConfig;
    /**
     * 支付宝client
     */
    private AlipayClient alipayClient;

    public AliPayMeta() {
    }

    public AliPayMeta(AliPayConfig.Config aliPayConfig) throws AlipayApiException {
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();

        certAlipayRequest.setServerUrl(aliPayConfig.getServerUrl());
        certAlipayRequest.setAppId(aliPayConfig.getAppId());
        certAlipayRequest.setPrivateKey(appRSAPrivateKey(aliPayConfig.getAppPrivateKeyPath()));
        certAlipayRequest.setFormat(aliPayConfig.getFormat());
        certAlipayRequest.setCharset(aliPayConfig.getCharset());
        certAlipayRequest.setSignType(aliPayConfig.getSignType());
        certAlipayRequest.setCertPath(getFileAbsolutePath(aliPayConfig.getAppCertPublicKeyPath()));
        certAlipayRequest.setAlipayPublicCertPath(getFileAbsolutePath(aliPayConfig.getAlipayPublicCertPath()));
        certAlipayRequest.setRootCertPath(getFileAbsolutePath(aliPayConfig.getAlipayRootCertPath()));
        this.alipayClient =  new DefaultAlipayClient(certAlipayRequest);
        this.aliPayConfig = aliPayConfig;
        this.certModel = true;
    }

    private String getFileAbsolutePath(String classPath) throws AlipayApiException {
        try {
            return new ClassPathResource(classPath).getFile().getAbsolutePath();
        } catch (IOException e) {
            LOG.error("ali pay cert path is not exist ,{}", e.getMessage());
            throw new AlipayApiException("ali pay cert path is not exist", e);
        }
    }


    private String appRSAPrivateKey(String classPath) throws AlipayApiException {
        ClassPathResource resource = new ClassPathResource(classPath);
        try {
            FileReader in = new FileReader(resource.getFile());
            try(BufferedReader bufferedReader = new BufferedReader(in)){
                return bufferedReader.readLine();
            }
        } catch (IOException e) {
            LOG.error("ali pay app private key is required ,{}", e.getMessage());
            throw new AlipayApiException("ali pay app private key is required", e);

        }
    }

}
